package com.xnfh.home.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.NzGlBean
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.home.R
import com.xnfh.home.databinding.FragmentAgriculturalGlBinding
import com.xnfh.home.ui.adapter.NzGlAdapter
import com.xnfh.home.viewmodel.AgriculturalGlViewModel

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/28 17:47
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/28 17:47
 * @UpdateRemark: 更新说明
 */
class AgriculturalGlFragment: BaseFragment<FragmentAgriculturalGlBinding>() {
    override fun getLayoutID(): Int = R.layout.fragment_agricultural_gl
    companion object {
        fun getInstance(type: Int?): AgriculturalGlFragment {
            val sf = AgriculturalGlFragment()
            sf.type = type
            return sf
        }
    }

    private var type: Int? = null
    private var page: Int = 1
    private var keywords: String? = null
    private var adapter: NzGlAdapter? = null
    private var viewModel: AgriculturalGlViewModel? = null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        if (type == null) type = 0
        mBind.agricultualRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = NzGlAdapter()
        mBind.agricultualRecycle.adapter = adapter
        mBind.agricultualRefresh.autoRefresh()
        mBind.agricultualRefresh.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel?.requestNzList(page,type,keywords)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel?.requestNzList(page,type,keywords)
            }
        })
    }

    override fun observe() {
        viewModel = AgriculturalGlViewModel()
        viewModel?.nzList?.observe(this,object : BaseStateObserver<NzGlBean>(){
            override fun getRespDataSuccess(it: NzGlBean) {
                super.getRespDataSuccess(it)
                mBind.agricultualRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if(it.total <= 0){
                    adapter?.setNewInstance(null)
                    adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1){
                    adapter?.setNewInstance(it.records.toMutableList())
                }else{
                    adapter?.addData(it.records)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.agricultualRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
    }
    fun setKeyWord(keywords: String?){
        this.keywords = keywords
        mBind.agricultualRefresh.autoRefresh()
    }
}